package libra;

import java.util.Map;

import javax.annotation.PostConstruct;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CookieValue;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import libra.openfeign.callLocal.OpenFeignClientLocal;
import libra.openfeign.callService.OpenFeignClientService;

/**
 * 外部Controller，通过该Controller触发OpenFeign的调用
 * 可以视为相当于是一个功能类似于SpringMVC.DispatcherServlet的请求分发器
 * @author libra
 */
@RestController
public class DispatcherController {
	@Autowired
	OpenFeignClientLocal local;
	@Autowired
	OpenFeignClientService service;
	
	/*********************** 测试OpenFeignClientLocal本地服务调用 **************************/
	// 测试请求头参数接收
	@RequestMapping("header")
	public Object header(@RequestHeader(value="testHeader",required = false) String header ) {
		return local.header(header);
	}
	
	// 测试请求cookie参数接收
	@RequestMapping("cookie")
	public Object cookie(@CookieValue(value="testCookie",required = false) String cookie ) {
		return local.cookie(cookie);
	}

	// 测试请求参数接收
	@RequestMapping("param")
	public Object param( String param ) {
		return local.param(param);
	}

	// 测试请求参数接收
	@RequestMapping("restParam/{val}")
	public Object restParam( @PathVariable("val")String val ) {
		return local.restParam(val);
	}

	// 测试请求参数接收
	@RequestMapping("json")
	public Object json( @RequestBody Map map ) {
		return local.json(map);
	}

	// 测试请求参数接收
	@RequestMapping("file")
	public Object file( MultipartFile file, String param ) {
		return local.file(file, param);
	}
	
	// 测试404请求(开启了@FeignClient的decode404)
	@RequestMapping("withDecode404")
	public Object withDecode404( ) {
		return local.withDecode404();
	}


	/*********************** 测试OpenFeignClientService微服务调用 **************************/
	// 测试服务调用请求
	@RequestMapping("exec")
	public Object exec() {
		return service.exec();
	}
	
	// 测试请求超时
	@RequestMapping("timeout")
	public Object timeout() {
		return service.timeout();
	}
	
	// 测试请求处理异常
	@RequestMapping("ex")
	public Object ex() {
		return service.ex();
	}
	
	// 测试404请求(未开启@FeignClient的decode404)
	@RequestMapping("withoutDecode404")
	public Object withoutDecode404( ) {
		return service.withoutDecode404();
	}




	@PostConstruct
	public void init() {
		System.out.println( "OpenFeignClientLocal的注入实例类型：" + local.getClass() );
		System.out.println( "OpenFeignClientService的注入实例类型：" + service.getClass() );
	}
}
